package com.ruoyi.web.controller.weChat;

import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.utils.URLUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/weChat/employee")
public class EmployeeController {

    public static void main(String[] args) throws Exception {
        String robit="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=9e6bc45a-62a5-4bc0-9008-11a1c90a52b5";
    senMsgByRobit("",robit);


    }


    //发送机器人消息
    public static void senMsgByRobit(String msg, String robit) throws Exception {
        // 获取部门列表
        String getPartUrl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=9e6bc45a-62a5-4bc0-9008-11a1c90a52b5";
        String postJson = "{\n" +
                "    \"msgtype\": \"markdown\",\n" +
                "    \"markdown\": {\n" +
                "        \"content\": \"印刷阶段：<font color=\\\"info\\\"> —印前— </font>\n" +
                "         >印刷流程：<font color=\\\"info\\\"> 排版 </font>\n" +
                "         >负责人:<font color=\\\"comment\\\">@何逸</font>\n" +
                "         >加工人:<font color=\\\"comment\\\">@王亚东</font>\n" +
                "         >预计耗时:<font color=\\\"warning\\\">6小时</font>\"\n" +
                "    }\n" +
                "}";


        String part1 = URLUtil.sendByPost(getPartUrl, postJson);
        System.out.println(part1);
    }

    //获取企微accesstToken
    public static String getWeChatAccessToken(String msg, String robit) throws Exception {

        String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?" +
                "corpid=ww63c6ad4d092b11a7&corpsecret=oJlbnseeJ0HMz7K58vaEwH3KrAEsnXBWQr5hcRwEZAA";
        String s = URLUtil.sendByGet(url);
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode accessTokenNode = objectMapper.readTree(s); // 将JSON字符串转换为JsonNode对象
        String accessToken = accessTokenNode.get("access_token").asText();
        System.out.println("access_token:" + accessToken); // 获取属性值并打印输出
        return accessToken;
    }


    //发送应用消息
    public static void sendMsgByApp(String msg, String accessToken) throws Exception {

        String getPartUrl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + accessToken;
        String postJson = "{\n" +
                "   \"msgtype\" : \"text\",\n" +
                "   \"agentid\" : 5629502339655787,\n" +
                "   \"content\" : {\n" +
                "       \"content\" : \"测试数据\\n你的快递已到，请携带工卡前往邮件中心领取。\\n出发前可查看<a href=\\\"http://work.weixin.qq.com\\\">邮件中心视频实况</a>，聪明避开排队。\"\n" +
                "   }\n" +
                "}";

        String part1 = URLUtil.sendByPost(getPartUrl, postJson);
        System.out.println(part1);

    }
}



